
expstack = []
pfixstack = []

def cmpopstack(op):
	if op in ["+","-"] and expstack[-1] in ["*","/"]:
		return True
	else:
		return False

expr = "a+b/c+5*(6+a)"

for token in expr:
	if token not in ["+","-","*","/","(",")"]:
		pfixstack.append(token)
	if token == "(" :
		expstack.append(token)
	if token == ")" :
		while len(expstack) !=0 and expstack[-1] is not "(":
			exprelemt = expstack.pop()
			pfixstack.append(exprelemt)
		x = expstack.pop() #Pop the left parenthesis from the stack and discard it 
		print " discard " + x
		
	if token in ["+","-","*","/"]:
		if len(expstack) == 0 or expstack[-1] == "(" :
			expstack.append(token)
		else:
			while len(expstack) != 0 and expstack[-1] != '(' and cmpopstack(token) :
				exprelemt = expstack.pop()
				pfixstack.append(exprelemt)
			expstack.append(token)

while len(expstack) != 0:
	exprelemt = expstack.pop()
	pfixstack.append(exprelemt)

print pfixstack

def evalPfixExpr(pfixstack):
	exprstack = []
	for token in pfixstack:
		if token not in ["+","-","*","/","(",")"]:
			exprstack.append(token)
		if token in ["+","-","*","/","(",")"]:
			a = exprstack.pop()
			b = exprstack.pop()
			result = b + " " + token + " " + a
			print result
			exprstack.append(result)
	print "value = " + exprstack.pop()

evalPfixExpr(pfixstack)		
